﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Tuto.Objects;
using System.Data;
namespace Tuto.BLL
{
    /// <summary>
    ///     Reprenetes business opereation on an TudoEtudiant object in the system
    /// </summary>
    public class TutoEtudiant : Objects.TutoPersonne
    {
        public int id;
        public string numeroCarte;
        public string diplome;
        public string niveau;

        public TutoEtudiant() : base()
        {
            // Nothig to do here for the moment ...
            // Normally, Etudiant creation logic is needed here.
        }
        
        /// <summary>
        ///     Constructs an objects with a given ID
        /// </summary>
        /// <param name="etudiantId"></param>
        public TutoEtudiant(int etudiantId) : base()
        {
            DAL.Etudiant dEtudiant = new DAL.Etudiant();

            DataSet _dataSet = dEtudiant.read(etudiantId);
            DataRow _dataRow = _dataSet.Tables[0].Rows[0];

            this.id = etudiantId;
            this.nom = _dataRow["nom"].ToString();
            this.prenom = _dataRow["prenom"].ToString();
            this.adresse = _dataRow["adresse"].ToString();
            this.email = _dataRow["email"].ToString();
            this.telephone = _dataRow["telephone"].ToString();
            this.numeroCarte = _dataRow["numero_carte"].ToString();
            this.diplome = _dataRow["diplome"].ToString();
            this.niveau = _dataRow["niveau"].ToString();

            _dataSet.Dispose();
        }

        /// <summary>
        ///     Creates a new 
        /// </summary>
        /// <returns></returns>
        public int create()
        {
            DAL.Etudiant dEtudiant = new DAL.Etudiant();
            int result = dEtudiant.create(this.doMapping());
            return result;
        }

        /// <summary>
        ///     Updates an Objects
        /// </summary>
        /// <returns></returns>
        public int update()
        {
            DAL.Etudiant dEtudiant = new DAL.Etudiant();
            
            int result = dEtudiant.update(this.doMapping(), this.id);
            
            return result;
        }

        /// <summary>
        ///     Deletes from the Database
        /// </summary>
        /// <returns></returns>
        public int delete()
        {
            DAL.Etudiant dEtudiant = new DAL.Etudiant();
            int result = dEtudiant.delete(this.id);
            return result;
        }
        /// <summary>
        ///     Does the mapping in the dictionary
        /// </summary>
        /// <param name="_obj"> The object to map </param>
        /// <returns> Returns a dictionary </returns>
        private Dictionary<string, object> doMapping()
        {
            Dictionary<string, object> fields = new Dictionary<string, object>();

            fields.Add("nom", this.nom);
            fields.Add("prenom", this.prenom);
            fields.Add("adresse", this.adresse);
            fields.Add("email", this.email);
            fields.Add("telephone", this.telephone);
            fields.Add("numero_carte", this.numeroCarte);
            fields.Add("diplome", this.diplome);
            fields.Add("niveau", this.niveau);

            return fields;
        }
    }
}
